package com.ruoyi.lab.service.impl;

import java.io.InputStream;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.lab.domain.LabSeatMessage;
import com.ruoyi.lab.enums.LabStatusEnum;
import com.ruoyi.lab.service.ILabSeatMessageService;
import com.ruoyi.lab.utils.RoleUtils;
import lombok.Cleanup;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.lab.mapper.LabMessageMapper;
import com.ruoyi.lab.domain.LabMessage;
import com.ruoyi.lab.service.ILabMessageService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 实验室信息Service业务层处理
 * 
 * @author dn
 * @date 2023-12-30
 */
@Service
@Slf4j
public class LabMessageServiceImpl implements ILabMessageService 
{
    @Autowired
    private LabMessageMapper labMessageMapper;

    @Autowired
    private ILabSeatMessageService labSeatMessageService;

    @Autowired
    private RoleUtils roleUtils;

    /**
     * 查询实验室信息
     * 
     * @param labId 实验室信息主键
     * @return 实验室信息
     */
    @Override
    public LabMessage selectLabMessageByLabId(Long labId)
    {
        return labMessageMapper.selectLabMessageByLabId(labId);
    }

    /**
     * 查询实验室信息列表
     * 
     * @param labMessage 实验室信息
     * @return 实验室信息
     */
    @Override
    public List<LabMessage> selectLabMessageList(LabMessage labMessage)
    {
        if (roleUtils.isStudent()) {
            // 学生只能查看已经开放的实验室
            labMessage.setLabStatus(LabStatusEnum.TOPIC_START.getStatus());
        }
        List<LabMessage> list = labMessageMapper.selectLabMessageList(labMessage);
        // 动态查询实验室的座位数量
        for (LabMessage lab : list) {
            LabSeatMessage labSeatMessage = new LabSeatMessage();
            labSeatMessage.setLabId(lab.getLabId());
            List<LabSeatMessage> seatMessageList = labSeatMessageService.selectLabSeatMessageList(labSeatMessage);
            if (seatMessageList.isEmpty()) lab.setCount(0);
            else lab.setCount(seatMessageList.size());
        }
        return list;
    }

    /**
     * 新增实验室信息
     * 
     * @param labMessage 实验室信息
     * @return 结果
     */
    @Override
    public int insertLabMessage(LabMessage labMessage)
    {
        labMessage.setLabId(System.currentTimeMillis());
        // 负责教师默认为自己
        labMessage.setChargeTeacherWorkno(SecurityUtils.getUsername());
        // 新添加的默认为草稿状态
        labMessage.setLabStatus(LabStatusEnum.TOPIC_DRAFT.getStatus());
        labMessage.setCreateBy(SecurityUtils.getUsername());
        labMessage.setCreateTime(DateUtils.getNowDate());
        return labMessageMapper.insertLabMessage(labMessage);
    }

    /**
     * 修改实验室信息
     * 
     * @param labMessage 实验室信息
     * @return 结果
     */
    @Override
    public int updateLabMessage(LabMessage labMessage)
    {
        LabMessage lab = labMessageMapper.selectLabMessageByLabId(labMessage.getLabId());
        // 普通教师不可以修改其他教师负责的实验室
        if (roleUtils.isTeacher() && !roleUtils.isAcademy()) {
            if (!SecurityUtils.getUsername().equals(lab.getChargeTeacherWorkno())) {
                return -1;
            }
        }

        // 院长只能修改自己学院的实验室信息
        if (roleUtils.isAcademy()) {
            if (!SecurityUtils.getDeptId().equals(lab.getDepartId())) {
                return -1;
            }
        }
        labMessage.setUpdateBy(SecurityUtils.getUsername());
        labMessage.setUpdateTime(DateUtils.getNowDate());
        return labMessageMapper.updateLabMessage(labMessage);
    }

    /**
     * 批量删除实验室信息
     * 
     * @param labIds 需要删除的实验室信息主键
     * @return 结果
     */
    @Override
    public int deleteLabMessageByLabIds(Long[] labIds)
    {
        // 如果该实验室中已经有座位，则实验室无法删除
        for (Long labId : labIds) {
            LabSeatMessage labSeatMessage = new LabSeatMessage();
            labSeatMessage.setLabId(labId);
            List<LabSeatMessage> seatMessageList = labSeatMessageService.selectLabSeatMessageList(labSeatMessage);
            if (seatMessageList.isEmpty()) {
                labMessageMapper.deleteLabMessageByLabId(labId);
            }
        }
        return 1;
    }

    /**
     * 删除实验室信息信息
     * 
     * @param labId 实验室信息主键
     * @return 结果
     */
    @Override
    public int deleteLabMessageByLabId(Long labId)
    {
        return labMessageMapper.deleteLabMessageByLabId(labId);
    }

    /**
     * 导入数据
     * @param file
     * @return
     */
    @Override
    public Boolean importData(MultipartFile file) {
        boolean flag = true;
        ExcelUtil<LabSeatMessage> util = new ExcelUtil<>(LabSeatMessage.class);
        try {
            @Cleanup
            InputStream inputStream = file.getInputStream();
            List<LabSeatMessage> list = util.importExcel(inputStream);

            for (LabSeatMessage labSeatMessage : list) {
                Long labId = labSeatMessage.getLabId();
                if (labId == null) {
                    log.error("所属实验室id不能为空");
                    flag = false;
                    continue;
                }
                LabMessage labMessage = labMessageMapper.selectLabMessageByLabId(labId);
                if (!SecurityUtils.getUsername().equals(labMessage.getChargeTeacherWorkno())) {
                    log.error("不是实验室负责人，无法导入");
                    flag = false;
                    continue;
                }
                String seatName = labSeatMessage.getSeatName();
                if (seatName == null) {
                    log.error("座位名不能为空");
                    flag = false;
                    continue;
                }
                seatName = seatName.replaceAll("[\t\n\r]","");
                labSeatMessage.setSeatName(seatName);
                labSeatMessageService.insertLabSeatMessage(labSeatMessage);
            }
        } catch (Exception e) {
            return false;
        }

        return flag;
    }
}
